﻿using Microsoft.AspNetCore.Mvc;
using Missbot.Admin.BLL.Svc;
using Missbot.AIModule.Sys;
using Missbot.Service;

namespace Missbot.Admin.Controllers
{
    [Route("[controller]/[action]")]
    public class VersionController : APIBaseController
    {
        public class GetVersionListReq
        {
            public int PageSize { get; set; }
            
            public int PageIdx { get; set; }
            
            public string VersionNo { get; set; }
        }

        [HttpPost]
        public IActionResult GetVersionList(GetVersionListReq req)
        {
            var filterBuilder = new FilterBuilder<VersionInfo>();
            if (!string.IsNullOrEmpty(req.VersionNo))
            {
                filterBuilder.And(p => p.Name == req.VersionNo);
            }

            var versions = MongoService<VersionInfo>.Get(filterBuilder.Filter, p => p.PublishDate, false, (req.PageIdx - 1) * req.PageSize, req.PageSize);
            return new JsonResult(versions);
        }

        [HttpPost]
        public IActionResult InsertOrUpdateVersion(VersionInfo version)
        {
            return string.IsNullOrEmpty(version.Id) ? InsertVersion(version) : UpdateVersion(version);
        }

        public IActionResult InsertVersion(VersionInfo version)
        {
            MongoService<VersionInfo>.Insert(version);
            return new JsonResult(new {code = 1});
        }

        public IActionResult UpdateVersion(VersionInfo version)
        {
            MongoService<VersionInfo>.Update(version);
            return new JsonResult(new {code = 1});
        }
    }
}